/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bits.db;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.Statement;

/**
 *
 * @author 3GBITS
 */
public class DBConnection {

    private String connURL = null;
    private Connection connection = null;
    private Statement statement = null;
    private ResultSet resultset = null;

    public DBConnection() {
        connURL = "jdbc:mysql://localhost/webhotel";
        System.out.println("Intializing the DB");
        initDB();
    }

    public void initDB() {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(connURL, "root", "redhat");
            statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            System.out.println("Connection Creted " + connection);
        } catch (Exception e) {

            try {
                System.out.println("Inside initDB() Exception");
                if (statement != null) {
                    statement.close();
                    statement = null;
                }
                if (connection != null) {
                    connection.close();
                    connection = null;
                }
                System.out.println("Inside initDB() " + e);
            } catch (Exception e1) {
                System.out.println("Exception in catch() of InitDB()" + e1);
            }
        }



    }

    public void insertOrUpdate(String query) {

        try {
            statement.executeUpdate(query);
        } catch (Exception e) {
            try {
                if (statement != null) {
                    statement.close();
                    statement = null;
                }
                if (connection != null) {
                    connection.close();
                    connection = null;
                }
                System.out.println("Inside insertOrUpdate() " + e);
            } catch (Exception e1) {
            }
        }
    }

    public ResultSet getResultSet(String query) {
        try {
            resultset = statement.executeQuery(query);
        } catch (Exception e) {
            try {
                if (statement != null) {
                    statement.close();
                    statement = null;
                }
                if (connection != null) {
                    connection.close();
                    connection = null;
                }
                System.out.println("Inside getResultSet() " + e);
            } catch (Exception e1) {
                System.out.println("connectDatabase.getResultSet()" + e1);
            }
        }
        return resultset;
    }

    public void connectionClose() {
        try {
            System.out.println("connectDatabase.connectionClose()" + connection);
            if (connection != null) {
                if (!connection.isClosed()) {
                    connection.close();
                    connection = null;
                }
            }

        } catch (Exception e) {
            System.out.println("connectDatabase.connectionClose()" + e);
        }
    }

    public void cleanAll(){
        System.out.println("In cleanAll()");
        try{
        if(resultset != null)
        {
            resultset.close();
            resultset = null;
        }
        if(statement != null){
            statement.close();
            statement = null;
        }if(connection != null){
            connectionClose();
        }
       
        }catch(Exception e){
            System.out.println("Inside cleanAll()"+e);
        }
    }
}
